var SP_NAMES;
var MULTIPLE;
var TEXT_PREFIX;
var TEXT_POSTFIX;
var lottery;
var bettingStatus;
var Results;
var ResultPanel;
var gameName;
var games;
var PAGE;
var oldOdds = null;
$(function() {
    if (template == "HK6") {
        $("#changlong").width("170px")
    }
    if (SP_NAMES) {
        $("#bet_panel th.tsname,.bet_content .tsname").each(function() {
            var n = $(this);
            var o = n.text();
            n.empty();
            n = $("<div>").appendTo(n);
            for (var m = 0, a = o.length; m < a; m++) {
                var p = o.charAt(m);
                n.append($("<span>").addClass("b" + p).text(p))
            }
        })
    }
    if (MULTIPLE) {
        $("#header .control").hide();
        $("#btnReset").attr("disabled", true).addClass("button_disabled");
        $("#btnBet").attr("disabled", true).addClass("button_disabled");
        var b;
        var c;
        var h = MULTIPLE[0];
        if (h == 0) {
            $("input[name=game]").click(function() {
                b = $(this).val().split(",");
                resetBets()
            }).attr("checked", false)
        } else {
            if (h == 1) {
                b = MULTIPLE[3];
                c = function(m, a) {
                    $(".bet_panel .odds:visible").hide();
                    if (m >= b[0] && m <= b[1]) {
                        $("#o_" + MULTIPLE[1] + "_" + m).show()
                    } else {
                        $("#empOdds").show()
                    }
                }
            } else {
                if (h == 2) {
                    b = MULTIPLE[3]
                } else {
                    if (h == 3) {
                        c = function(o, n, m) {
                            var a = true;
                            $(".status_panel table").each(function() {
                                if ($(this).find("input:checked").length == 0) {
                                    a = false;
                                    return false
                                }
                            });
                            $(".bcontrol input:text").prop("disabled", !a);
                            if (MULTIPLE[3]) {
                                $(".status_panel input[value=" + m.val() + "]").not(m).prop("disabled", n)
                            }
                        }
                    }
                }
            }
        }
        $(".status_panel .check").click(function(n) {
            if (!bettingStatus) {
                return false
            }
            var m = $(this).find("input");
            if (m.prop("disabled")) {
                return false
            }
            var a = m.prop("checked");
            var o = $(".status_panel input:checked").length;
            if (n.target == this) {
                a = !a;
                if (a) {
                    o += 1
                }
            }
            if (!a) {
                o -= 1
            }
            m.prop("checked", a);
            if (c) {
                c(o, a, m)
            }
            $(this).toggleClass("selected", a);
            if (b) {
                $(".bcontrol input:text").prop("disabled", (o < b[0] || o > b[1]));
                if (o >= b[1]) {
                    $(".status_panel input:not(:checked)").prop("disabled", true)
                } else {
                    $(".status_panel input:disabled").prop("disabled", false)
                }
            }
        })
    } else {
        function g(n) {
            var m = PeriodPanel.period;
            if (!m || m.status != 1) {
                return
            }
            var a = getBet(n);
            if (a) {
                parent.showBets({
                    lottery: lottery,
                    drawNumber: m.drawNumber,
                    bets: [a]
                })
            }
        }
        $("#bet_panel tr:not(.head)").each(function() {
            var a = $(this);
            a.find("input.ba").each(function() {
                var n = $(this).attr("name");
                var m = $("#bet_panel .G" + n);
                m.hover(function() {
                    m.addClass("hover")
                }, function() {
                    m.removeClass("hover")
                });
                if (!$(this).hasClass("empty")) {
                    m.click(function(r) {
                        if (!bettingStatus) {
                            return
                        }
                        var q = $(r.target);
                        if (q.filter("input").length) {
                            q.val($("label.quickAmount input").val())
                        } else {
                            m.toggleClass("selected");
                            var p = $("#bet_panel th.selected").length;
                            var o = $("#betcount").text(p);
                            if (p == 0) {
                                o.parent().hide()
                            } else {
                                o.parent().show()
                            }
                            if ($(this).hasClass("selected")) {
                                m.find("input").val($("label.quickAmount input").val())
                            } else {
                                m.find("input").val("")
                            }
                        }
                    })
                }
            })
        });
        $("#bet_panel .panel_yxx a").click(function() {
            var a = $(this).attr("id").substr(2);
            g(a)
        });
        var k = $("label.quickAmount input");
        k.keyup(function() {
            k.not(this).val($(this).val());
            if (parent) {
                parent.QuickAmount = $(this).val()
            }
            $("#bet_panel tr:not(.head)").each(function() {
                var a = $(this);
                a.children("th").each(function() {
                    var n = $(this).attr("id").substr(2);
                    var m = $("#bet_panel .G" + n);
                    if ($(this).hasClass("selected")) {
                        m.find("input").val(k.val())
                    }
                })
            })
        });
        var e = $("label.checkdefault input");
        e.click(function() {
            var a = $(this).prop("checked");
            e.not(this).prop("checked", a);
            if (parent) {
                parent.QuickAmountEnalbed = a;
                parent.QuickAmount = k.val()
            }
        });
        if (parent && parent.QuickAmountEnalbed) {
            e.prop("checked", true);
            k.val(parent.QuickAmount)
        }
    }
    var d = true;
    var l = {
        titleConverter: function(a) {
            if (a.indexOf("冠亚") == 0 && a.indexOf("-") == -1) {
                a = a.replace("冠亚", "冠亚 - ")
            } else {
                a = a.replace("-", " - ")
            }
            return a
        },
        interval: 10,
        changlong: $("#changlong tbody"),
        onChangLongClick: function(a) {
            var m = PeriodPanel.period;
            if (m != null && m.status == 1) {
                $(".G" + a).addClass("selected").find("input").focus()
            }
        },
        onPeriodChange: function(n, a) {
            var m = n != null && n.status == 1;
            toggleBet(m);
            if (m && !a && location.hash && typeof window.IS_MOBILE == "undefined") {
                $(".G" + location.hash.substr(1)).addClass("selected").find("input").focus()
            }
        },
        onResultChange: function() {
            if (Results) {
                Results.load()
            }
            if (ResultPanel) {
                ResultPanel.load()
            }
        },
        onAccountUpdated: function(a) {
            if (parent && parent.showAccount) {
                parent.showAccount(a)
            }
        },
        loadOptions: {
            url: "/index.php/Odds/index",
            data: {
                lottery: lottery,
                games: games
            },
            success: function(a) {
				//alert(a.1z_0);
                showOdds(a)
            }
        }
    };
    $.each(["cdOpen", "cdClose", "cdDraw"], function(m, a) {
        l[a] = $("#" + a)
    });
    PeriodPanel.init(l, gameName);
    if (window.parent && window.parent != window) {
        var f = PAGE || LIBS.getUrlParam("page");
        var j = LIBS.getUrlParam("index");
        var i = window.parent.$(".sub div:visible a").removeClass("selected").each(function() {
            var n = $(this);
            var a = n.attr("href");
            var o = LIBS.getUrlParam("page", a);
            var m = LIBS.getUrlParam("index", a);
            if (o == f && (!j || !m || m == j)) {
                if (!gameName) {
                    $("#gameName").text(n.text())
                }
                n.addClass("selected");
                return false
            }
        })
    }
    $("#quickControl").click(function() {
        $("#quickPanel").toggle($(this).prop("checked"))
    });
    $("input:text").keypress(function(a) {
        if (a.keyCode == 13) {
            bet();
            return false
        }
    }).onlyNumber();
    toggleBet(false)
});
function showOdds(a) {
    var e = PeriodPanel.period;
	//alert(e);
    if (!a || e == null || e.status != 1) {
        oldOdds = null;
        toggleBet(false)
    } else {
        toggleBet(true);
        for (var b in a) {
            var d = a[b];
			
            if (d) {
                var c = $("#o_" + b).text(d);
                if (oldOdds && d != oldOdds[b]) {
                    c.delayClass("uptodate", 3000)
                }
            } else {
                $("#o_" + b).text("--")
            }
        }
        oldOdds = a
    }
}
function showBetting(a) {
    if (a) {
        $("#bettingStatus").show();
        $("#betControl").hide()
    } else {
        $("#bettingStatus").hide();
        $("#betControl").show()
    }
}
function sortBets(a) {
    a.sort(function(f, d) {
        if (f.title != d.title) {
            return 0
        }
        var e = (Number(f.contents) == f.contents);
        var c = (Number(d.contents) == d.contents);
        if (e && !c) {
            return -1
        }
        if (c && !e) {
            return 1
        }
        if (e && c) {
            return f.contents - d.contents
        }
        return 0
    });
    return a
}
function toggleBet(a) {
    if (bettingStatus != null && bettingStatus === a) {
        return
    }
    bettingStatus = a;
    if (!a) {
        if (typeof window.IS_MOBILE !== "undefined") {}
        $("#bet_panel").addClass("bet_closed");
        $("#bet_panel .odds:not(.empty)").text("--");
        if (MULTIPLE) {
            $(".status_panel input").prop("disabled", true)
        } else {
            $("#bet_panel .amount input").prop("disabled", true)
        }
        resetBets()
    } else {
        $("#bet_panel").removeClass("bet_closed");
        if (MULTIPLE) {
            $(".status_panel input").prop("disabled", false)
        } else {
            $("#bet_panel .amount input").prop("disabled", false)
        }
    }
}
function resetBets() {
    if (typeof window.IS_MOBILE !== "undefined") {
        resetMobileAll()
    }
    if (MULTIPLE) {
        $(".status_panel .check").removeClass("selected");
        $(".status_panel input:checked").prop("checked", false);
        $(".bcontrol input:text").prop("disabled", true);
        if (bettingStatus) {
            $(".status_panel input:disabled").prop("disabled", false)
        }
        if (MULTIPLE[0] == 1) {
            $(".bet_panel .odds:visible").hide();
            $("#empOdds").show()
        } else {
            if (MULTIPLE[0] == 3) {
                $("#fs_odds").text("--")
            }
        }
    } else {
        $("#betcount").parent().hide();
        $("#bet_panel .selected").removeClass("selected");
        $("#bet_panel input.ba").val("")
    }
    if (!$("label.checkdefault input").is(":checked")) {
        $("label.quickAmount input").val("")
    }
}
function getBet(g, j) {
    if (!j) {
        j = $("#t_" + g)
    }
    var c = $("#o_" + g);
    var l = Number(c.text());
    if (isNaN(l) || l <= 0) {
        return
    }
    var f = j.attr("title");
    f = f.split(" ", 2);
    var h = f[1];
    f = (f.length > 1) ? f[0] : "";
    var d = g.split("_");
    var b = {
        title: f,
        text: h,
        game: d[0],
        contents: d[1],
        odds: l
    };
    var a = Number(c.attr("lang"));
    if (a > 0) {
        var k = [];
        for (var e = 1; e < a; e++) {
            k[e - 1] = $("#o_" + g + "_" + e).text()
        }
        b.oddsDetail = k
    }
    return b
}
function getMultipleBets() {
    var k = Math.floor(Number($(".bcontrol .quickAmount input").val()));
    if (k <= 0 || isNaN(k)) {
        return false
    }
    var m = MULTIPLE[0];
    var p = MULTIPLE[1];
    var n = MULTIPLE[2];
    var b = MULTIPLE[3];
    var e;
    if (m == 0) {
        var q = $("input[name=game]:checked");
        if (q.length == 0) {
            return false
        }
        n = q.attr("title");
        p = q.attr("id").substr(2);
        b = q.val().split(",")
    } else {
        if (m == 1) {
            e = $(".status_panel input:checked");
            p += "_" + e.length
        }
    }
    var r = Number($("#o_" + p).text());
    var a = [];
    var c = p.split("_");
    function l(v, t, s, i, u) {
        a.push({
            multiple: s,
            title: n,
            text: v,
            game: c[0],
            contents: t,
            odds: u || r,
            state: c[1] || 0,
            mcount: i,
            amount: k
        })
    }
    if (m == 3) {
        var o = [];
        $(".status_panel table").each(function() {
            var i = [];
            $(this).find("input:checked").each(function() {
                i.push($(this).val())
            });
            i.sort();
            o.push(i)
        });
        o = LIBS.comboList(o);
        for (var h = 0; h < o.length; h++) {
            var j = o[h];
            l(j, j.join(","), 1)
        }
    } else {
        if (m == 2) {
            var e = $(".status_panel input:checked");
            var o = [];
            var d = {};
            var g = {};
            e.each(function() {
                var i = $(this).val();
                o.push(i);
                d[i] = $(this).attr("title");
                g[i] = Number($("#o_" + p + "_" + i).text())
            });
            o = o.sort();
            o = LIBS.comboArray(o, b[0]);
            for (var h = 0; h < o.length; h++) {
                var j = o[h];
                l(LIBS.replaceArray(j, d), j.join(","), 1, b[0], LIBS.replaceArray(j, g).sort(function(s, i) {
                    return MULTIPLE[4] ? s - i : i - s
                }).shift())
            }
        } else {
            if (!e) {
                e = $(".status_panel input:checked")
            }
            var o = [];
            var d = {};
            e.each(function() {
                var i = $(this).val();
                o.push(i);
                d[i] = $(this).attr("title")
            });
            o = o.sort();
            var f = b[0];
            if (m == 1) {
                f = e.length
            }
            l(LIBS.replaceArray(o, d), o.join(","), LIBS.combination(e.length, f), f)
        }
    }
    return a
}
function getBets() {
    var a = [];
    $("#bet_panel input.ba").each(function() {
        var d = Number($(this).val());
        if (d <= 0 || isNaN(d)) {
            return
        }
        var c = $(this).attr("name");
        var b = getBet(c);
        if (b) {
            b.amount = d;
            a.push(b)
        }
    });
    return a
}
function bet() {
    var h = PeriodPanel.period;
    if (!h || h.status != 1) {
        parent.showMsg("后台未开盘，请等待开盘再试。");
        return
    }
    var c;
    if (MULTIPLE) {
        c = getMultipleBets()
    } else {
        c = getBets()
    }
    if (c === false) {
        return
    }
    if (c.length == 0) {
        parent.showMsg("您选择的项目已封盘或输入的金额不正确，请重新选择。");
        return
    }
    resetBets();
	
    for (var e = 0; e < c.length; e++) {
        var a = c[e];
		//alert($("#k_" + a.game + "_" + a.contents).val());
        var g = parent.getUserParam(lottery, $("#k_" + a.game + "_" + a.contents).val());
        if (g) {
            var f = false;
            var d = a.amount;
            if (d < g.minAmount) {
                f = "你输入的金额低于单注最低(" + g.minAmount + ")的限制！"
            } else {
                if (d > g.maxAmount) {
                    f = "你输入的金额超出单注最高(" + g.maxAmount + ")的限制！"
                }
            }
            if (f) {
                parent.showMsg(f);
                return
            }
        }
    }
    parent.showBets({
        lottery: lottery,
        drawNumber: h.drawNumber,
        bets: c
    })
}
;